package com.astrocsr.action.home;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.astrocsr.business.StatementManager;
import com.astrocsr.business.UserManager;
import com.astrocsr.common.Constants;
import com.astrocsr.common.helper.ConfigHelper;
import com.astrocsr.common.helper.ContextHelper;
import com.astrocsr.common.util.DateUtil;
import com.astrocsr.domain.model.ReportDownloadModel;
import com.astrocsr.domain.web.Item;
import com.astrocsr.domain.web.UserAccount;
import com.astrocsr.domain.web.UserInfo;
import com.opensymphony.xwork2.ModelDriven;

/**
 * 
 * @author Ho.Viet.Duan Created Jan 08, 2014
 */
@Controller("reportDownloadAction")
@Scope("prototype")
public class ReportDownloadAction extends BaseAction implements
		ModelDriven<ReportDownloadModel> {

	private static final long serialVersionUID = 1L;
	private ReportDownloadModel model = new ReportDownloadModel();

	public ReportDownloadModel getModel() {
		return model;
	}

	@Autowired
	private UserManager userManager;
	@Autowired
	private StatementManager statementManager;

	static final Logger log = Logger.getLogger(ReportDownloadAction.class);

	/**
	 * Initial Data for reportDownload
	 */
	public String reportDownload() throws Exception {
		try {
			// Build latest 3 months
			int numOfMonths = Integer.valueOf(ConfigHelper.getInstance().get(Constants.REPORT_NUM_OF_MONTHS));
			model.setItemList(userManager.buildMonths(numOfMonths));
			UserInfo userInfo = model.getUserInfo(); 
			if (userInfo != null && userInfo.getUserId() != null) {
				userInfo = userManager.getUserById(userInfo.getUserId());
			} else {
				userInfo = userManager.getUserByEmail(ContextHelper.getLoginId());
			}
			model.setUserInfo(userInfo);
			
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			throw e;
		}
		return SUCCESS;
	}
	
	/**
	 * Search Statement Date in Special Month
	 */
	public String executeSearchStatementDateInMonth() throws Exception {
		try {
			String selectedMonth = model.getSelectedMonth();
			UserInfo userInfo    = model.getUserInfo();
			if (StringUtils.isNotBlank(selectedMonth) && selectedMonth.split("-").length ==2
					&& userInfo != null && userInfo.getUserId() != null) {
				String[] smArr = selectedMonth.split("-");
				//String[] accArr = {"086332626-2", "081843909-9", "085397736-7"};
				System.out.println("UserId = " + userInfo.getUserId());
				List<UserAccount> userAccountList = userManager.listUserAccount(userInfo.getUserId());
				
				Date date = DateUtil.createDateLocal(1, Integer.valueOf(smArr[0]), Integer.valueOf(smArr[1]));
				List<Item> stmtDateList =statementManager.executeSearchStatementDateInMonth(userAccountList, date);
				
				model.setStmtDateList(stmtDateList);
			}
		} catch (Exception e) {
			log.error(e.getMessage(), e);
		} catch (Throwable e) {
			log.error(e.getMessage(), e);
		}
		return SUCCESS;
	}
}